﻿using HuiLian.BigData.Hdl.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http.Headers;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HuiLian.BigData.Hdl
{
    public partial class LoginForm : Form
    {
        private readonly HttpHelper _httpHelper;

        public LoginForm(HttpHelper httpHelper)
        {
            _httpHelper = httpHelper;

            InitializeComponent();
        }

        private async void BtnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(TextAccount.Text))
            {
                MessageBox.Show("请输入账号");
                return;
            }
            if (string.IsNullOrWhiteSpace(TextPassword.Text))
            {
                MessageBox.Show("请输入密码");
                return;
            }
            GlobalVariable.IsLocalLogin = LocalLogin.Checked;
            if (LocalLogin.Checked)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                try
                {
                    //var result = await _httpHelper.LoginAsync(TextAccount.Text, TextPassword.Text);

                    //if (result.Code == 1)
                    //{
                    //    GlobalVariable.Token = result.Token;
                    this.DialogResult = DialogResult.OK;
                    //}
                    //else
                    //{
                    //    MessageBox.Show("错误：" + result.Msg);
                    //}
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误：" + ex.Message);
                }
            }

            //this.DialogResult = DialogResult.OK;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            this.TextAccount.Text = "admin";
            this.TextPassword.Text = "111111";

            //HttpC();
        }

        public async void HttpC()
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri("http://10.1.8.81:9080/hdl/oauth/v1.0/access.json"),
                Headers =
    {
        { "Host", "scada" },
        { "Authorization", "Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJzNW10RC1mdzJUSjh0bE9YY3JMZ3U0ZExZTHZWdVBCM0VkQmdpek80WGxZIn0.eyJleHAiOjE3MzI3MjI1NDksImlhdCI6M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PAhXb-b9WBVNxl7ufLU1RqOuATRkAojqy4vEUPAfDFmDS0bB_5Kpf14pJi2G0s6bWD58kTUaovWY099qTWJ-2s0OUP6Hl66DkgqpqRYu8w" },
    },
                Content = new StringContent("{\r\n    \"client_id\":\"g2xmb\",\r\n    \"client_secret\":\"ce934e0a8c672844b7d6ec8e4205b0cfdea5586ef6ca9a5cd626e170b4574061\",\r\n    \"grant_type\":\"client_credentials\"\r\n}")
                {
                    Headers =
        {
            ContentType = new MediaTypeHeaderValue("application/json")
        }
                }
            };
            using (var response = await client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();
                var body = await response.Content.ReadAsStringAsync();
                Console.WriteLine(body);
                MessageBox.Show(body);
            }
        }
    }
}
